package 蓝桥算法;

public class day02 {
    public int[] sortedQuares(int[] nums){//利用双指针法排序
        int[] b=new int[nums.length];
        int k=nums.length-1;
        for(int i=0,j=nums.length-1;i<=j;){
            if((nums[i]*nums[i])>(nums[j]*nums[j])){
                b[k--]=(nums[i]*nums[i]);
                i++;
            }
            else {
                b[k--]=(nums[j]*nums[j]);
                j--;
            }
        }
        return b;
    }
    public int minsubArraylen(int[] nums,int s){//滑动窗口求取长度最小的子数组
        int result=nums.length-1;
        int k=nums.length-1;
        int i=0;
        int sum=0;
        for(int j=0;j<nums.length-1;j++){
            sum+=nums[j];
            while(sum>=s){
                int subl=j-i+1;
                result=Math.min(result,subl);
                sum=sum-nums[i];
                i++;
            }
        }
        return result;
    }
    public static void main(String[] args) {
        day02 a=new day02();
        int[] nums={-3,-2,2,5,6,8,9};
        int[]b=new int[nums.length];
        b=a.sortedQuares(nums);
        for (int i = 0; i < b.length; i++) {
            System.out.println(b[i]);
        }
        int x=a.minsubArraylen(nums,11);
        System.out.println(x);
    }

}
